package com.tqw.item.controller;



import com.tqw.entity.Result;
import com.tqw.entity.StatusCode;
import com.tqw.item.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.tqw.vo.ImageVO;

/**
 * 处理图片上传
 * @author Alex
 */
@RestController
@RequestMapping("/item/file")
public class FileController {

    @Autowired
    private FileService fileService;


    /**
     * 图片上传
     * 请求路径:/file/upload
     * 请求类型:post
     * 请求参数:file
     * 返回值:Result
     * @return
     */
    @PostMapping("/upload")
    public Result<ImageVO> upload(MultipartFile file){

        ImageVO imageVO = fileService.upload(file);
        Result<ImageVO> result = new Result<>();
        if (imageVO==null){
            result.setCode(StatusCode.ERROR);
            return result;
        }
        System.out.println(imageVO);
        result.setData(imageVO);
        System.out.println(result.getCode());
        return result;
    }

    /**
     * 删除图片
     * 请求路径:/file/deleteFile
     * 请求类型:delete
     * 请求参数:virtuaPath
     * 返回值:SysResult
     * @return
     */
    @DeleteMapping("/deleteFile")
    public Result<?> deleteFile(String virtualPath){

        fileService.deleteFile(virtualPath);
        return new Result<>();

    }

}
